package com.mooneil.sadness.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.mooneil.sadness.domain.GuestbookArticle;
import com.mooneil.sadness.service.GuestbookService;

@Controller
public class GuestbookController {

	@Autowired
	private GuestbookService guestbookService;

	@RequestMapping(value = "Guestbook", method = RequestMethod.GET)
	public ModelAndView onGuestbookRequest() {

		ModelAndView guestbookMNV = new ModelAndView("Guestbook",
				"articleList", guestbookService.getGuestbookArticleList());

		guestbookMNV.addObject(new GuestbookArticle());
		return guestbookMNV;

	}

	@RequestMapping(value = "SubmitArticle", method = RequestMethod.POST)
	public ModelAndView onSubmitArticle(
			@ModelAttribute GuestbookArticle guestbookArticle,
			HttpServletResponse response) throws IOException {

		guestbookService.addGuestbookArticle(guestbookArticle);

		response.sendRedirect("Guestbook");
		return null;

	}

}
